<?php



require 'always.php';

// Below is a very simple PHP 5 script that implements the RPX token URL processing.
// The code below assumes you have the CURL HTTP fetching library.  

$rpxApiKey = '51c2c9604564232e1abd9a06d02b34603837cd6c';  

if(isset($_POST['token'])) { 

  /* STEP 1: Extract token POST parameter */
  $token = $_POST['token'];

  /* STEP 2: Use the token to make the auth_info API call */
  $post_data = array('token' => $_POST['token'],
                     'apiKey' => $rpxApiKey,
                     'format' => 'json'); 

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_URL, 'https://rpxnow.com/api/v2/auth_info');
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
  curl_setopt($curl, CURLOPT_HEADER, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  $raw_json = curl_exec($curl);
  curl_close($curl);


  /* STEP 3: Parse the JSON auth_info response */
  $auth_info = json_decode($raw_json, true);

  if ($auth_info['stat'] == 'ok') {
  
        /* STEP 3 Continued: Extract the 'identifier' from the response */
        $profile = $auth_info['profile'];
        $identifier = $profile['identifier'];
        $_SESSION['token']=$identifier;

        if (isset($profile['photo']))  {
          $photo_url = $profile['photo'];
          $_SESSION['photo_url']=$photo_url;
        }

        if (isset($profile['displayName']))  {
          $name = $profile['displayName'];
          $_SESSION['displayName']=$name;
        }

        if (isset($profile['email']))  {
          $email = $profile['email'];
          $_SESSION['email']=$email;
        }
    
        if(isset($_SESSION['previousPage'])) {
            header("Location: ".$_SESSION['previousPage']);        
        } else {
            header("Location: /index.php");
        }


    /* an error occurred */
    } else {
    // gracefully handle the error.  Hook this into your native error handling system.
    echo 'An error occured: ' . $auth_info['err']['msg'];
    }
} else {
    echo("Disculpa las molestias, ha ocurrido un error en la autenticación");
}

?>